package cn.thr.ymcc.service.impl;

import cn.thr.ymcc.constant.Cos;
import cn.thr.ymcc.domain.MediaFile;
import cn.thr.ymcc.service.MediaProducer;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Service
public class MediaProducerImpl implements MediaProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    /**
     * 异步推流：同步发送
     * @param mediaFile
     * @return
     */
    @Override
    public boolean syncSend(MediaFile mediaFile) {
        SendResult sendResult = rocketMQTemplate.syncSend(
                Cos.VideoStream.TOPIC_COURSE_VIDEO_PUSH_STREAM + ":" + Cos.VideoStream.TAGS_COURSE_VIDEO_PUSH_STREAM,
                MessageBuilder.withPayload(JSON.toJSONString(mediaFile)).build()
        );
        return sendResult.getSendStatus() == SendStatus.SEND_OK;
    }
}
